<?php

namespace App\Http\Controllers\Admin\UserManager;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;    //引用http请求类
use App\Response\Response;
use Illuminate\Support\Facades\Validator;

class UserAuthController extends BaseController {

    /**
     * @api {post} /user-manager/user-auth/single/check 身份认证审核-审核
     * @apiVersion 0.1.0
     * @apiName 会员管理-身份认证审核-审核
     * @apiGroup 会员管理
     *
     * @apiParam {int} id
     * @apiParam {string} reason                       原因
     * @apiParam {number} app_status                   1通过 2拒绝
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function check(Request $request) {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'id' => 'required|integer',
                'reason' => 'filled|string',
                'app_status' => 'required|numeric|in:1,2',
            ]);
        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));
        $params = $request->input();
        $id = $params['id'];
        if ($params['app_status'] == 2 && empty($params['reason'])) {
            throw new BasicException(10001, "拒绝时必填原因！");
        }

        $rows = app('repo_user')->checkUserAuth($id, $params);
        if (!$rows) {
            throw new BasicException(10001, "设置失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {post} /user-manager/user-auth/single/cancel 身份认证审核-取消
     * @apiVersion 0.1.0
     * @apiName 会员管理-身份认证审核-取消
     * @apiGroup 会员管理
     * @apiParam {int} id
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function cancel(Request $request) {

        $params = $request->all();
        //参数校验
        $v = app('validator')->make($params,
            [
                'id' => 'required|integer',
//                'reason' => 'filled|string',
//                'app_status' => 'required|numeric|in:1,2',
            ]);
        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $id = $params['id'];

        //参数校验
        $rows = app('repo_user')->cancelUserAuth($id, $request->input());
        if (!$rows) {
            throw new BasicException(10001, "设置失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {get} /user-manager/user-auth 身份认证审核-列表
     * @apiVersion 0.1.0
     * @apiName 会员管理-身份认证审核-列表
     * @apiGroup 会员管理
     *
     * @apiParam {number} sex 性别 1：男 2：女 可选
     * @apiParam {number} app_status 审核状态0审核中1通过2不通过 可选
     * @apiParam {string} nickname 用户昵称 可选
     * @apiParam {number} user_code 用户编号 可选
     * @apiParam {number} page 当前页数 可选
     * @apiParam {number} perPage 分页容量 可选
     *
     *
     * @apiSuccessExample 成功响应
     *
     *
    {
    "code": 200,
    "data": {
        "list": [
            {
                "full_name(姓名)": "王伟",
                "id_number(身份证号)": "42092219731015461X",
                "id_card(照片1)": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/img/0c06d48731bf532b9569efbbc050443d.jpg",
                "front(照片2)": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/img/258c792d56e4829223463796e83786ab.jpg",
                "back(照片3)": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/img/258c792d56e4829223463796e83786ab.jpg",
                "app_status(状态 审核状态0审核中1通过2不通过)": 1,
                "reason(拒绝原因)": "",
                "created_time(提交时间)": null,
                "nickname(昵称)": "嘿嘿嘿嘿",
                "user_code(用户id)": 514853,
            }
        ],
        "meta": {
                "page": 1,
                "limit": 10,
                "next": null,
                "total": 1
            }
        }
     }
     *
     *
     */
    public function list(Request $request) {
        //参数校验
//        try {
            $validator= Validator::make($request->all(), [
                'sex' => 'filled|numeric|in:1,2',
                'app_status' => 'filled|numeric|in:0,1,2,3,4',
                'perPage' => 'filled|numeric',
                'page' => 'filled|numeric',
                'start_time' => 'filled|date',
                'end_time' => 'filled|date',
            ]);
            if ($validator->fails()) {
                throw new BasicException(10001, $validator->errors()->first());
            }
//        } catch (\Exception $e) {
//            throw new BasicException(10001, json_encode($e->errors()));
//        }
        $params = $request->input();
        $params['role_id'] = $request->role_id;
        $list = app('repo_user')->getUserAuthQueryList($params);

        return Response::sendList($list);
    }

    /**
     * @Desc:实名封禁或者解封
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/11/2 16:17
     */
    public function ban(Request $request)
    {
        $params = $request->only(['id', 'type', 'reason', 'url', 'source']);
        $validator= Validator::make($params, [
            'id' => 'required|integer',
            'type' => 'required|integer|in:0,1',//0=封禁;1=解封
            'reason' => 'filled|string|max:255',
            'url' => 'filled|array',
        ]);
        if ($validator->fails()) {
            throw new BasicException(10001, $validator->errors()->first());
        }
        $params['admin_id'] = $request->admin_id;
        app('repo_user')->userAuthBan($params);

        return Response::sendData(true);
    }

    /**
     * @Desc:获取用户信息
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/11/2 16:35
     */
    public function userInfo(Request $request)
    {
        $params = $request->only(['id']);
        $userInfo = app('repo_user')->userInfo($params);
        return Response::sendData($userInfo);
    }

    /**
     * @Desc:解绑
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/11/2 16:49
     */
    public function userAuthUntie(Request $request)
    {
        $params = $request->only(['id', 'reason']);
        $validator= Validator::make($params, [
            'id' => 'required|integer',
            'reason' => 'required|string|max:255',
        ]);
        if ($validator->fails()) {
            throw new BasicException(10001, $validator->errors()->first());
        }
        $params['admin_id'] = $request->admin_id;
        app('repo_user')->userAuthUntie($params);
        return Response::sendData(true);
    }

    /**
     * @Desc:封禁记录
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/11/3 11:25
     */
    public function banLog(Request $request)
    {
        $params = $request->only(['id']);
        $banLog = app('repo_user')->banLog($params);
        return Response::sendList($banLog);
    }
}
